<?php

/**
 * IndexController - The default controller class
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class Adminb_AccountController extends App_Zend_Controller_Action_Adminb 
{
	

	
	public function init(){
		parent::init();
		$member=new CMember();
		
		if(!$member->isUser($this->_request->getModuleName(),$this->_request->getControllerName())){
			
			if($_COOKIE[COOKIE_PRE.'uid']!=''){
				
				$this->_redirect(MY_URL.'/buyer');
				
			}else{
			
				$this->_redirect(MY_URL.'/buyer-auth-login?preurl=');
				
			}
			
		}
		
	}
	
	public function preDispatch()
	{

		$this->getHelper ('layout')->setLayout('framelayout');
		
		$this->view->addHelperPath(MY_ROOT.'/vhelpers');
		
		Zend_Controller_Action_HelperBroker::addPath(MY_ROOT.'/chelpers');
		
		$this->view->assign('currentAction',$this->_request->getActionName());
			
	}
	

   	public function indexAction(){
   		
   		$order=new COrder();
   		
   		$orderArray=$order->getAllOrders(0,0,$_COOKIE[COOKIE_PRE.'uid']);
   		
   		$disputeAccount=0;
   		
   		foreach ($orderArray as $value){
   			
   			if($value['flid']=='6'){
   			
   				$disputeAccount+=($value['money']-$value['shipping_cost'])*$value['discount'];
   				
   			}
   			
   		}
   		
   		$disputeAccount=round($disputeAccount,2);
   		
   		$this->view->assign('disputeAccount',$disputeAccount);
   		
   		$account=new CAccount();
   		
   		$accountArray=$account->getAllAccounts(0,0,"from_uid='".$_COOKIE[COOKIE_PRE.'uid']."'");
   		
   		$buyAccount=0;
   		
   		$receiveAccount=0;
   		
   		$buyBalance=0;
   		
   		$actualReceiveAccount=0;
   		
   		$actualRequireAccount=0;
   		
   		foreach ($accountArray as $value){
    		
    		if($value['flid']=='1'){
    		
    			$buyAccount+=$value['has_paid'];
    		
    		}else if($value['flid']=='2'){

    			$receiveAccount+=$value['has_paid'];
    			
    		}else if($value['flid']=='5'){

    			$actualReceiveAccount+=$value['has_paid'];
    			
    			$actualRequireAccount+=$value['should_paid'];
    			
    		}
    			
    	}
    	
    	$buyBalance=$buyAccount-$actualReceiveAccount;
    	
    	$this->view->assign('buyAccount',$buyAccount);
    	
    	$this->view->assign('receiveAccount',$receiveAccount);
    	
    	$this->view->assign('actualRequireAccount',$actualRequireAccount);
    	
    	$this->view->assign('actualReceiveAccount',$actualReceiveAccount);
    	
    	$this->view->assign('buyBalance',$buyBalance);
    	
    	$member=new CMember();
   		
    	$memberArray=$member->getUserDetails(array('uid'=>$_COOKIE[COOKIE_PRE.'uid']));
    	
    	$this->view->assign('memberArray',$memberArray);
    	$integral = $memberArray[0]['integral'];
    	$score = new CScore();
    	$dollar = $score->score2dollar($integral);
    	$this->view->integral = $integral;
    	$this->view->dollar = $dollar;
    	
    	$uid = isset($_COOKIE[COOKIE_PRE.'uid'])?$_COOKIE[COOKIE_PRE.'uid']:0;
    	
    	$mlm = new CMlm();
    	$this->view->accountAmount = $mlm->getAllCash($uid);
   	}
   	
   	public function itemAction(){
   		
   		$page=$this->_request->getParam("page",1);
		
		$account=new CAccount();
		
		$counts=$account->getAllAccountCounts('('.R_PRE."accounts.flid IN(3,5,1,6) ) AND (".R_PRE."accounts.from_uid='".$_COOKIE[COOKIE_PRE.'uid']."' OR ".R_PRE."accounts.to_uid='".$_COOKIE[COOKIE_PRE.'uid']."')");
		
    	Zend_Loader::loadClass("CFilter");
    	
    	$pageArray = CFilter::filterPage ( $page, $counts, 10 );
		
		$itemArray=$account->getAllAccounts($pageArray['from'],10,'('.R_PRE."accounts.flid IN(3,5,1,6)) AND (".R_PRE."accounts.from_uid='".$_COOKIE[COOKIE_PRE.'uid']."' OR ".R_PRE."accounts.to_uid='".$_COOKIE[COOKIE_PRE.'uid']."')");
		
		Zend_Loader::loadClass("CPage");
    	
    	$pageString = CPage::divPagesAdmin( $pageArray ['pageNumber'], $pageArray ['page'], ROOT_PATH."/adminb-account-item?"); //生成分页字符串
    	
    	$this->view->assign ( "pageString", $pageString );
    	
    	$this->view->assign ( "pageArray", $pageArray );
    	
    	$this->view->assign ( 'itemArray', $itemArray );
   		
   	}
   	
   	public function moreAction(){
   		
   		$accountid=$this->_request->getParam("accountid");
   		
   		$flid=$this->_request->getParam("flid");
   		
   		if($flid=='3'){
   		
   			$account=new CAccount();
   			
   			$accountArray=$account->getDetail($accountid);
   			
   			$orderidArray=unserialize($accountArray[0]['orderid_string']);
   			
   			$this->view->assign ( 'orderidArray', $orderidArray );
   			
   			$this->view->assign ( 'accountArray', $accountArray );
   			
   		}else if($flid=='5'){
   			
   			$account=new CAccount();
   			
   			$accountArray=$account->getDetail($accountid);
   			
   			$orderidArray=unserialize($accountArray[0]['orderid_string']);
   			
   			$this->view->assign ( 'orderidArray', $orderidArray );
   			
   			$this->view->assign ( 'accountArray', $accountArray );
   			
   		}else if($flid=='1'){
   			
   			$account=new CAccount();
	   		
	   		$accountArray=$account->getDetail($accountid);
	   		
	   		$orderidArray=unserialize($accountArray[0]['orderid_string']);
	   		
	   		$order=new COrder();
	   		
	   		$orderArray=array();
	   		
	   		foreach ($orderidArray as $value){
	   			
	   			$orderArray[]=$order->getDetail($value);
	   			
	   		}
	   		
	   		$this->view->assign ( 'orderArray', $orderArray );
	   		
	   		$this->view->assign ( 'accountArray', $accountArray );
   			
   		}else if($flid=='6'){
   			
   			$account=new CAccount();
	   		
	   		$accountArray=$account->getDetail($accountid);
	   		
	   		$this->view->assign ( 'accountArray', $accountArray );
   			
   		}
   		
   		$this->render('more'.$flid);
   		
   	}
   	
   	public function reimburseAction(){
   		
   		$this->_helper->viewRenderer->setNoRender();
   		
   		$orderid=$this->_request->getParam("orderid");
   		
   		$paypal=$this->_request->getParam("paypal");
   		
   		$payment=$this->_request->getParam("payment");
   		
   		if($payment=='paypal'){
   		
   			$member=new CMember();
   		
   			$member->updateUserDetail(array('paypal'=>$paypal),array('uid'=>$_COOKIE[COOKIE_PRE.'uid']));
   			
   		}
   		
   		$order=new COrder();

   		$orderArray=$order->getDetail($orderid);
   		
   		if($orderArray[0]['flid']!='6'){
   			
   			$this->view->showMsg("Something wrong happens!");
   			
   			return false;
   			
   		}else{
   		
	   		$orderid_string=serialize(array($orderid));
	   		
	   		$account=new CAccount();
	   		
	   		$accountArray=$account->getAccountDetails(array('orderid_string'=>$orderid_string,'flid'=>'5'));
	   		
	   		if(count($accountArray)>0){
	   			
	   			$this->view->showMsg("You have required the reimbursement of this order!");
	   			
	   			return false;
	   			
	   		}else{
	   			
	   			$paramArray=$this->_request->getParams();
		   		
		   		$filters = array(
		    		'*'   => 'StringTrim'
				);
		
				$validators = array(
				);
				
				$input = new Zend_Filter_Input($filters, $validators, $paramArray);
	   		
	   			$shouldReturn=round(($orderArray[0]['money']-$orderArray[0]['shipping_cost'])*$orderArray[0]['discount']+$orderArray[0]['shipping_cost'],2);
	   			
		   		$data=array(
		   					'should_paid'=>$shouldReturn,
		   					'has_paid'=>0,
		   					'flid'=>5,
		   					'state'=>0,
		   					'orderid_string'=>$orderid_string,
		   					'from_uid'=>$_COOKIE[COOKIE_PRE.'uid'],
		   					'to_uid'=>'0',
		   					'add_time'=>time(),
		   					'payment'=>$payment,
		   					'remark'=>$input->content
		   					);
		   					
		   		$account->createAccount($data);
				
				$orderRemark=new COrderRemark();
				
				$orderRemarkArray=$orderRemark->getRemarkDetails(array('orderid'=>$orderid,'flid'=>2));

				
				if(count($orderRemarkArray)>0){
					
					$orderRemark->editRemark($orderRemarkArray[0]['remarkid'],array('content'=>$input->content,'add_time'=>time()));
					
				}else{
				
					$orderRemark->createOrderRemark(array('orderid'=>$orderid,'content'=>$input->content,'flid'=>2,'add_time'=>time()));
		   		
				}
				
				$member=new CMember();
		
				$memberArray=$member->getUserDetails(array('uid'=>$_COOKIE[COOKIE_PRE.'uid']));
				
				$fenlei=new CFenlei('order_remark');
				
				$remarkContent=$fenlei->getFenLeiNameByFlid('2');
				
				$input->content=$input->content.'<br /><a href="administrator-order-more?orderid='.$input->orderid.'">查看订单</a>';
				
				$data=array(
		    				'from_uid'=>$_COOKIE[COOKIE_PRE.'uid'],
		    				'to_uid'=>2,
		    				'add_time'=>time(),
		    				'subject'=>'会员'.$memberArray[0]['username'].'对订单#'.$orderid.'进行操作',
		    				'content'=>$remarkContent.':'.$input->content,
		    				'has_read'=>0,
							'from_has_del'=>'1',
		    				'flid'=>'2',
		    			   );
		    			   
		    	$pm=new CPm();
		    	
		    	$pm->createPm($data);
					
		   		$this->view->showMsg("Your requirement of reimbursement has been submitted!",$_SERVER['HTTP_REFERER']);
		   					
		   		return true;
	   		
	   		}
	   		
   		}
   		
   	}

   	public function matchAction()
   	{
   		$page = $this->_request->getParam("page", 1);        
        $chipin = new CChipin();
        $counts = $chipin->getCounts(array('UID'=>$_COOKIE[COOKIE_PRE . 'uid']));
        Zend_Loader::loadClass("CFilter");
        $pageArray = CFilter::filterPage($page, $counts, 10);
        $resultArray = $chipin->getAllByUser($pageArray['from'],10,array('UID'=>$_COOKIE[COOKIE_PRE . 'uid']));
        Zend_Loader::loadClass("CPage");
        $pageString = CPage::divPagesAdmin($pageArray ['pageNumber'], $pageArray ['page'], ROOT_PATH . "/adminb-account-match"); //生成分页字符串
        $matchInfo = new CMatchinfo();
        $score = new CScore();
        $this->view->matchInfo = $matchInfo;
        $this->view->pageString = $pageString;
        $this->view->resultArray = $resultArray;
        $this->view->pageArray = $pageArray;
        $this->view->score = $score;
   	}
   	
   	public function clubAction()
   	{
   		$member = new CMember();
   		$memberInfo = $member->getUserDetails(array('uid'=>$_COOKIE[COOKIE_PRE . 'uid']));
   		if ($memberInfo[0]['clubid']) {
   			$matchInfo = new CMatchinfo();
   			$team = $matchInfo->getTeam($memberInfo[0]['clubid']);
   			$this->view->team = $team;
   			$this->view->hasClub = 1;
   		} else {
   			$fenlei = new CFenlei('product');
			$clubList = $fenlei->getFenleiByPid(92);
			$this->view->clubList = $clubList;
   			$this->view->hasClub = 0;
   		}
   	}
   	
   	function doclubAction()
   	{
   		if ($this->getRequest()->isPost()) {
   			$hasClub = $this->getRequest()->getParam('has_club');
   			$member = new CMember();
   			if ($hasClub == 1) {   				
   				$member->updateUserDetail(array('clubid'=>0), array('uid'=>$_COOKIE[COOKIE_PRE . 'uid']));
   				$fenlei = new CFenlei('product');
				$fenlei->refreshIntegral();
   				$this->view->showMsg('You exit.');
   			} elseif ($hasClub == 0) {
   				$clubid = intval($this->getRequest()->getParam('clubid'));
   				$member->updateUserDetail(array('clubid'=>$clubid), array('uid'=>$_COOKIE[COOKIE_PRE . 'uid']));
   				$fenlei = new CFenlei('product');
				$fenlei->refreshIntegral();
				$this->view->showMsg('You Join.');
   			}
   		}
   	}
	
	public function postAction(){
	
		//$this->_helper->viewRenderer->setNoRender();
		
		//$this->getHelper ('layout')->disableLayout ();
	
	}
    

    
}
